/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se.jocke.nbrs.ui.action;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.WindowManager;
import se.jocke.nbrs.model.RestServiceGroup;
import se.jocke.nbrs.service.RestServiceManager;
import se.jocke.nbrs.ui.window.RestServiceTreeTopComponent;

/**
 *
 * @author jocke
 */
public class AddNewRestServiceAction extends AbstractAction {
    
    public AddNewRestServiceAction() {
        super("Add new rest service");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        NotifyDescriptor.InputLine discriptor = new NotifyDescriptor.InputLine(
                "Create a new rest service",
                "New rest service",
                NotifyDescriptor.OK_CANCEL_OPTION,
                NotifyDescriptor.INFORMATION_MESSAGE);
            
        if (DialogDisplayer.getDefault().notify(discriptor) == NotifyDescriptor.OK_OPTION && discriptor.getInputText() != null && !discriptor.getInputText().isEmpty()) {
            
            RestServiceTreeTopComponent component = (RestServiceTreeTopComponent) WindowManager.getDefault().findTopComponent(RestServiceTreeTopComponent.PREF_ID);
            
            RestServiceGroup group = component.getLookup().lookup(RestServiceGroup.class);
            
            if(group != null) {
                Lookup.getDefault().lookup(RestServiceManager.class).addNewService(group.getName(), discriptor.getInputText());
            }
                      
        }

    }

}
